package com.youku.plugins.shareV4.view.components
{
   import flash.display.Sprite;
   import flash.display.Bitmap;
   import flash.text.TextField;
   import com.youku.interfaces.IPlayerProxy;
   import flash.display.Loader;
   import flash.utils.Timer;
   import com.youku.interfaces.IPlayListData;
   import com.youku.PlayerConfig;
   import flash.events.TimerEvent;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import flash.system.LoaderContext;
   import flash.text.TextFieldAutoSize;
   import com.youku.plugins.shareV4.ShareFont;
   import flash.text.StyleSheet;
   import flash.events.TextEvent;
   
   public class QRCodeBox extends Sprite
   {
      
      public function QRCodeBox(param1:IPlayerProxy) {
         super();
         this._playerProxy = param1;
         this.init();
      }
      
      private static const QRCODE_URL:String = "http://qrcode.ykimg.com/qr?size=90&logosize=30";
      
      private var _qrImage:Bitmap;
      
      private var _qrBackground:Sprite;
      
      private var _erroTf:TextField;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _loader:Loader;
      
      private var _timer:Timer;
      
      private var _from:String;
      
      private var _time:Number;
      
      private var _retryTimer:Timer;
      
      private var _retryTimeOut:Number = 1000.0;
      
      private function get playlistData() : IPlayListData {
         return PlayerConfig.playListData;
      }
      
      private function init() : void {
         this._from = "";
         this._time = 0;
         this._retryTimer = new Timer(this._retryTimeOut);
         this._retryTimer.addEventListener(TimerEvent.TIMER,this.onRetryToShow);
         this._qrBackground = new Sprite();
         this.addChild(this._qrBackground);
         this._loader = new Loader();
         this._timer = new Timer(5 * 1000);
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onError);
         this._timer.addEventListener(TimerEvent.TIMER,this.onError);
      }
      
      public function loadQRCode(param1:String, param2:Number) : void {
         this._qrBackground.visible = false;
         this.drawQRBackground();
         this._from = param1;
         this._time = param2;
         if(this._qrImage)
         {
            if(this._qrBackground.contains(this._qrImage))
            {
               this._qrBackground.removeChild(this._qrImage);
            }
            this._qrImage = null;
         }
         this.getQRCodeFormUrl(param1,param2);
      }
      
      private function getQRCodeFormUrl(param1:String, param2:Number) : void {
         var _loc3_:String = QRCODE_URL;
         _loc3_ = _loc3_ + ("&t=http://v.youku.com/v_show/id_" + this.playlistData.vidEncoded + ".html");
         if(param1 == "preview")
         {
            _loc3_ = _loc3_ + ("?firsttime=" + param2);
         }
         this._loader.load(new URLRequest(_loc3_),new LoaderContext(true));
         this._timer.reset();
         this._timer.start();
      }
      
      private function onComplete(param1:Event) : void {
         this._timer.stop();
         this._qrBackground.visible = true;
         if(this._qrImage)
         {
            if(this._qrBackground.contains(this._qrImage))
            {
               this._qrBackground.removeChild(this._qrImage);
            }
            this._qrImage = null;
         }
         this._qrImage = this._loader.content as Bitmap;
         this._qrImage.width = 90;
         this._qrImage.height = 90;
         try
         {
            this._qrImage.smoothing = true;
         }
         catch(e:Error)
         {
         }
         this._qrBackground.addChild(this._qrImage);
         this.updateLayout();
      }
      
      private function drawQRBackground() : void {
         this._qrBackground.graphics.clear();
         this._qrBackground.graphics.beginFill(16777215);
         this._qrBackground.graphics.drawRect(0,0,94,94);
         this._qrBackground.graphics.endFill();
      }
      
      private function onRetryToShow(param1:TimerEvent) : void {
         this._retryTimer.stop();
         this.loadQRCode(this._from,this._time);
      }
      
      private function drawQRErrorBackground() : void {
         this._qrBackground.graphics.clear();
         this._qrBackground.graphics.lineStyle(1,16777215);
         this._qrBackground.graphics.moveTo(0,0);
         this._qrBackground.graphics.lineTo(94,0);
         this._qrBackground.graphics.lineStyle(1,16777215);
         this._qrBackground.graphics.moveTo(93,1);
         this._qrBackground.graphics.lineTo(93,93);
         this._qrBackground.graphics.lineStyle(1,16777215);
         this._qrBackground.graphics.moveTo(94,93);
         this._qrBackground.graphics.lineTo(0,93);
         this._qrBackground.graphics.lineStyle(1,16777215);
         this._qrBackground.graphics.moveTo(0,1);
         this._qrBackground.graphics.lineTo(0,93);
      }
      
      private function onError(param1:Event) : void {
         this._timer.stop();
         this._qrBackground.visible = true;
         if(this._qrImage)
         {
            if(this._qrBackground.contains(this._qrImage))
            {
               this._qrBackground.removeChild(this._qrImage);
            }
            this._qrImage = null;
         }
         this.drawQRErrorBackground();
         this._erroTf = new TextField();
         this._erroTf.autoSize = TextFieldAutoSize.LEFT;
         this._erroTf.defaultTextFormat = ShareFont.FONT_COMMON;
         var _loc2_:StyleSheet = new StyleSheet();
         _loc2_.parseCSS("a{color:#06a7e1;}a:hover{color:#cc3311;}");
         this._erroTf.styleSheet = _loc2_;
         var _loc3_:* = "二维码获取\n失败" + "请<u><a href=\'event:geturl|{url}\'>重试<a></u>";
         this._erroTf.htmlText = _loc3_;
         this._erroTf.addEventListener(TextEvent.LINK,this.onRetry);
         this._qrBackground.addChild(this._erroTf);
         this.updateLayout();
      }
      
      private function onRetry(param1:TextEvent) : void {
         if(this._qrBackground.contains(this._erroTf))
         {
            this._qrBackground.removeChild(this._erroTf);
            this._erroTf = null;
         }
         this._retryTimer.reset();
         this._retryTimer.start();
      }
      
      private function updateLayout() : void {
         if(this._qrImage)
         {
            this._qrImage.x = int((this._qrBackground.width - this._qrImage.width) / 2);
            this._qrImage.y = int((this._qrBackground.height - this._qrImage.height) / 2);
         }
         if(this._erroTf)
         {
            this._erroTf.x = 10;
            this._erroTf.y = 22;
         }
      }
   }
}
